package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class MovieAdapter extends ArrayAdapter<Movie> {

    private int resourceId;

    public MovieAdapter(Context context, int resource, List<Movie> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Movie movie = getItem(position);
        View view;
        ViewHolder viewHolder;

        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.movieImage = view.findViewById(R.id.img_movie);
            viewHolder.movieTitle = view.findViewById(R.id.text_movie);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.movieImage.setImageResource(movie.getImageResourceId());
        viewHolder.movieTitle.setText(movie.getTitle());

        return view;
    }

    class ViewHolder {
        ImageView movieImage;
        TextView movieTitle;
    }
}    